<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel="stylesheet" type="text/css" href="../js/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../js/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo/demo.css">
	<script type="text/javascript" src="../js/jquery.min.js"></script>
	<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript">

	$(function(){
		
		
		var rows= [
			{
			"chances": [],
			"name": "系统管理员",
			"password": "admin",
			"status": 1,
			"userId": 1,
			"userName": "admin"
			},
			{
			"chances": [],
			"name": "于经理",
			"password": "123456",
			"status": 1,
			"userId": 2,
			"userName": "yulibin"
			},
			{
			"chances": [],
			"name": "郝经理",
			"password": "123456",
			"status": 1,
			"userId": 3,
			"userName": "haominghui"
			},
			{
			"chances": [],
			"name": "吴经理",
			"password": "123456",
			"status": 1,
			"userId": 4,
			"userName": "wumingliang"
			}
			];
			
			
	    $('#tt1').datagrid({
	        rownumbers:true,
	        width:660,
	        height:250,
	        singleSelect:true,
	        checkOnSelect: true,
	        columns:[[
	            {field:'a',title:'Item ID',width:60,checkbox: true},
	            {field:'cname',title:'name',width:60,editor: { type: 'text', options: { required: true } }},
	            {  
	                field: 'userId',  
	                title: 'date',  
	                width: 100,  
	        
	                editor: {  
	                    type: 'combobox',  
	                    options: {  
	                        valueField: 'userId',  
	                        textField: 'userName',  
	                        data: rows,  
	                        required: true  
	                    }  
	                }  
	            },  
	            {field:'action',title:'Action',width:70,align:'center',
	                formatter:function(value,row,index){
	                    if (row.editing){
	                        var s = '<a href="#" onclick="saverowb(this)">Saves</a> ';
	                        var c = '<a href="#" onclick="cancelrowb(this)">Cancels</a>';
	                        return s+c;
	                    } else {
	                        var e = '<a href="#" onclick="editrowb(this)">Edits</a> ';
	                        var d = '<a href="#" onclick="deleterowb(this)">Deletes</a>';
	                        return e+d;
	                    }
	                }
	            }
	        ]],
	        onBeforeEdit:function(index,row){
	            row.editing = true;
	            updateActionsb(index);
	        },
	        onAfterEdit:function(index,row){
	            row.editing = false;
	            updateActionsb(index);
	        },
	        onCancelEdit:function(index,row){
	            row.editing = false;
	            updateActionsb(index);
	        }
	    });
	});
//===================================================================================
	function updateActionsb(index){
	    $('#tt1').datagrid('updateRow',{
	        index: index,
	        row:{}
	    });
	}
	
	function getRowIndexb(target){
	    var tr = $(target).closest('tr.datagrid-row');
	    return parseInt(tr.attr('datagrid-row-index'));
	}
	function editrowb(target){
	    $('#tt1').datagrid('beginEdit', getRowIndexb(target));
	}
	function deleterowb(target){
	    $.messager.confirm('Confirm','Are you sure?',function(r){
	        if (r){
	            $('#tt1').datagrid('deleteRow', getRowIndexb(target));
	        }
	    });
	}
	function saverowb(target){
	    $('#tt1').datagrid('endEdit', getRowIndexb(target));
	}
	function cancelrowb(target){
	    $('#tt1').datagrid('cancelEdit', getRowIndexb(target));
	}
	function insertb(){
		alert(1)
		var row = $('#tt1').datagrid('getSelected');
		if (row){
			var index = $('#tt1').datagrid('getRowIndex', row);
		} else {
			index = 0;
			alert(2)
		}
		$('#tt1').datagrid('insertRow', {
			index: index,
			row:{
				status:'P'
			}
		});
		alert(3)
		$('#tt1').datagrid('selectRow',index);
		alert(4)
		$('#tt1').datagrid('beginEdit',index);
		alert(5);	
	}
//==================================================================================	
	
			function updateActions(index){
					    $('#tt').datagrid('updateRow',{
					        index: index,
					        row:{}
					    });
					}
					
					function getRowIndex(target){
					    var tr = $(target).closest('tr.datagrid-row');
					    return parseInt(tr.attr('datagrid-row-index'));
					}
					function editrow(target){
					    $('#tt').datagrid('beginEdit', getRowIndex(target));
					}
					function deleterow(target){
					    $.messager.confirm('Confirm','Are you sure?',function(r){
					        if (r){
					            $('#tt').datagrid('deleteRow', getRowIndex(target));
					        }
					    });
					}
					function saverow(target){
					    $('#tt').datagrid('endEdit', getRowIndex(target));
					}
					function cancelrow(target){
					    $('#tt').datagrid('cancelEdit', getRowIndex(target));
					}
					
					function insert(){
						alert(1)
						var row = $('#tt').datagrid('getSelected');
						if (row){
							var index = $('#tt').datagrid('getRowIndex', row);
						} else {
							index = 0;
						}
						alert(2)
						$('#tt').datagrid('insertRow', {
							index: index,
							row:{
								status:'P'
							}
						});
						alert(3)
						$('#tt').datagrid('selectRow',index);
						alert(4)
						$('#tt').datagrid('beginEdit',index);
						alert(5)
					}
	
//===================================================================================	
	
    </script>
    
    
    
    <script type="text/javascript">
				$(function(){
				
					loadTable();
				
			
				});	
				function loadTable(){
					//自定义1个combobox编辑器
					$.extend($.fn.datagrid.defaults.editors, {
					
						    combogrid: {			
						        init: function(container, options){			
						            var input = $('<input type="text" class="datagrid-editable-input">').appendTo(container);
						            input.combogrid(options);		
						            return input;			
						        },
					
						        destroy: function(target){			
						            $(target).combogrid('destroy');		
						        },
					
						        getValue: function(target){			
						            return $(target).combogrid('getValue');		
						        },
						
						        setValue: function(target, value){		
						            $(target).combogrid('setValue', value);			
						        },
						
						        resize: function(target, width){		
						            $(target).combogrid('resize',width);			
						        }
						
						    }
						
						});

					
					
					var ds =[
				{

				"ystype": "1",
				"ysname": "应收"
				},
				{
					"ystype": "0",
					"ysname": "应付"
				}
				];
					
					var as="";
					var xiaofeishuju ="";
					
						$.ajax({
							type:"GET",
					 		async: false,
							url:'<%=path%>/main/get/subinfo',
							dateType:'json',
							data:null,
							success:function(data){
								var result = eval('(' + data + ')');
								as=result.data;
							
								}
							});
						
						$.ajax({
							type:"GET",
					    	async: false,
					   	 	url:"<%=path %>/main/get/xiaofei",
					   		data :null,
					   		dataType: "json",
					    	success:function(data){
					    		console.log(data);					    		
					    		xiaofeishuju = data.data;		    		
						    	
					    	}
						});
						
						
					
					$('#tt').datagrid({
						
						width:924,
						height:350,
						singleSelect:true,
						rownumbers:true,
						selectOnCheck: true,
					    checkOnSelect: true,
						title:'',	
						columns:[[
						{field:'id',title:'序号',width:190,checkbox:true},
			            {  
			                field: 'xiaofei',  
			                title: '消费大类',  
			                width: 100,  
			                formatter: function(value) {  
			                    for (var i = 0; i < xiaofeishuju.length; i++) {  
			                        if (xiaofeishuju[i].code == value){ 
			                        	
			                        	return xiaofeishuju[i].codename
			                        };  
			                    }  
			                    return value;  
			                },  
			                editor: {  
			                    type: 'combobox',  
			                    options: {  
			                        valueField: 'code',  
			                        textField: 'codename',  
			                        data: xiaofeishuju,  
			                        required: true,
			                        onSelect:function(rowIndex, rowData){
			                  
			                        	var row = $("#tt").datagrid('getSelected');
			                        	var rindex = $("#tt").datagrid('getRowIndex', row);
			                   
			                        	var ed = $("#tt").datagrid('getEditor', {
			                                index: rindex,
			                                field: 'xiaofei'
			                            });
			                        	  var s = $(ed.target).combobox('getText');
			                        
			                        	$.ajax({
			                				type:"GET",
			                		 		async: false,
			                				url:'<%=path%>/main/get/zixiaofei',
			                				dateType:'json',
			                				data:{"iname":s},
			                				success:function(data){
			                					
			                					var result = eval('(' + data + ')');
			              					
			                					console.log(result.data);
			                					var editor = $("#tt").datagrid("getEditor",{index:rindex,field:'xiangmu'});
			                					$(editor.target).combobox("loadData",result.data);
			                			
			                					}
			                				});
			                        	}
			                    }  
			                }  
			            },
			            {  
			                field: 'xiangmu',  
			                title: '项目',  
			                width: 100,  
			                formatter: function(value) {  
			                    for (var i = 0; i < xiaofeishuju.length; i++) {  
			                        if (xiaofeishuju[i].code == value){ 
			                        	
			                        	return xiaofeishuju[i].codename
			                        };  
			                    }  
			                    return value;  
			                },  
			                editor: {  
			                    type: 'combobox',  
			                    options: {  
			                        valueField: 'code',  
			                        textField: 'codename',  
			                        data: xiaofeishuju,  
			                        required: true,			  
			                    }  
			                }  
			            },
			            {field:'mingxi',title:'项目明细',width:195,editor: { type: 'text', options: { required: true } }},
						{field:'shoushudc1',title:'手术医师',name:'shoushudc',width:100,            	
            	            editor:{            	
            	                type:'combogrid',           	
            	                options:{            
            	                    panelWidth:200,            	
            	                    idField:'cPersonCode',            
            	                    textField:'cPersonName',          
            	                    url:'<%=path%>/main/get/persons',  
            	                    columns:[[
            	                        {field:'cDepCode',title:'部门id',width:60,checkbox:true},
            	                        {field:'cPersonCode',title:'职员编码',width:60},           
            	                        {field:'cPersonName',title:'职员姓名',width:100},
            	                    ]]
            
            	                }
            
            	            }
            	        },
            			{field:'shoushudc2',title:'配台医师',name:'shoushudc',width:100,            	
            	            editor:{            	
            	                type:'combogrid',           	
            	                options:{            
            	                    panelWidth:200,            	
            	                    idField:'cPersonCode',            
            	                    textField:'cPersonName',          
            	                    url:'<%=path%>/main/get/persons',  
            	                    columns:[[
            	                        {field:'cDepCode',title:'部门id',width:60,checkbox:true},
            	                        {field:'cPersonCode',title:'职员编码',width:60},           
            	                        {field:'cPersonName',title:'职员姓名',width:100},
            	                    ]]
            
            	                }
            
            	            }
            	        },
            			{field:'shoushudc3',title:'非手术医师',name:'shoushudc',width:100,            	
            	            editor:{            	
            	                type:'combogrid',           	
            	                options:{            
            	                    panelWidth:200,            	
            	                    idField:'cPersonCode',            
            	                    textField:'cPersonName',          
            	                    url:'<%=path%>/main/get/persons',  
            	                    columns:[[
            	                        {field:'cDepCode',title:'部门id',width:60,checkbox:true},
            	                        {field:'cPersonCode',title:'职员编码',width:60},           
            	                        {field:'cPersonName',title:'职员姓名',width:100},
            	                    ]]
            
            	                }
            
            	            }
            	        },
            			{field:'shoushudc4',title:'医师助手',name:'shoushudc',width:100,            	
            	            editor:{            	
            	                type:'combogrid',           	
            	                options:{            
            	                    panelWidth:200,            	
            	                    idField:'cPersonCode',            
            	                    textField:'cPersonName',          
            	                    url:'<%=path%>/main/get/persons',  
            	                    columns:[[
            	                        {field:'cDepCode',title:'部门id',width:60,checkbox:true},
            	                        {field:'cPersonCode',title:'职员编码',width:60},           
            	                        {field:'cPersonName',title:'职员姓名',width:100},
            	                    ]]
            
            	                }
            
            	            }
            	        },
            	        {field:'biaojia',title:'标价',width:195,editor: { type: 'text', options: { required: true } }},
            	        {field:'shuliang',title:'数量',width:195,editor: { type: 'text', options: { required: true } }},
            	        {field:'zhekou',title:'折扣',width:195,editor: { type: 'text', options: { required: true } }},
            	        {field:'zhehoupr',title:'折后价格',width:195,editor: { type: 'text', options: { required: true } }},
            	        {field:'beizhu',title:'备注',width:195,editor: { type: 'text', options: { required: true } }},	
				        {field:'action',title:'Action',width:90,align:'center',
			                formatter:function(value,row,index){
			                    if (row.editing){
			                        var s = '<a onclick="saverow(this)">Save</a> ';
			                        var c = '<a href="#" onclick="cancelrow(this)">Cancel</a>';
			                        return s+c;
			                    } else {
			                        var e = '<a href="#" onclick="editrow(this)">Edit</a> ';
			                        var d = '<a href="#" onclick="deleterow(this)">Delete</a>';
			                        return e+d;
			                    }
			                }
			            }
						          ]],
						          onBeforeEdit:function(index,row){
							            row.editing = true;
							            updateActions(index);
							        },
							        onAfterEdit:function(index,row){
							            row.editing = false;
							            updateActions(index);
							        },
							        onCancelEdit:function(index,row){
							            row.editing = false;
							            updateActions(index);
							        }
					});			
				}
				
	</script>
    
<title>Insert title here</title>
	

</head>
<body>



		
<div>
	<div style="margin-left:0px" id="dd1">
			<a href="javascript:void(0)" class="easyui-linkbutton l-btn l-btn-small" onclick="insertb()">
				<span class="l-btn-left">
					<span class="l-btn-text">charu</span>
				</span>
			</a>
	</div>
			<table id="tt1">
						
			</table>
		
	</div>
		
		
		
		
		<div>
	<div style="margin-left:0px" id="dd1">
			<a href="javascript:void(0)" class="easyui-linkbutton l-btn l-btn-small" onclick="insert()">
				<span class="l-btn-left">
					<span class="l-btn-text">Insert row</span>
				</span>
			</a>
		</div>
		<table id="tt">
			
		</table>
	<div class="combined"><label>合计</label><input type="text" value="" /></div>
</div>
</body>
</html>